package org.ww.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.ww.edu.bean.Chapter;
import org.ww.edu.bean.Video;
import org.ww.edu.bean.vo.ChapterVo;
import org.ww.edu.bean.vo.VideoVo;
import org.ww.edu.mapper.ChapterMapper;
import org.ww.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.ww.edu.service.VideoService;
import org.ww.servicebase.exceptionhandler.GuliException;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ww
 * @since 2020-10-23
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;

    // 获取所有的章节小节信息
    @Override
    public List<ChapterVo> getAllChapterVideo(String courseId) {
        //1 获取所有的章节信息
        QueryWrapper<Chapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id",courseId);
        List<Chapter> chapterList = baseMapper.selectList(chapterWrapper);

        //2 获取所有的小节信息
        QueryWrapper<Video> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id",courseId);
        List<Video> videoList = videoService.list(videoWrapper);

        //创建用于最终存储的集合
        List<ChapterVo> finalList=new ArrayList<>();
        //3 循环遍历章节信息进行封装
        for (int i = 0; i < chapterList.size(); i++) {
            //获取所有的章节对象
            Chapter chapter = chapterList.get(i);
            //创建封装的chapter对象
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            //将对象放入集合中
            finalList.add(chapterVo);

            //创建用于存储小节对象的集合
            List<VideoVo> videoVoList=new ArrayList<>();

            //4 循环遍历小节信息进行封装
            for (int j = 0; j < videoList.size(); j++) {
                //获取所有的小节对象
                Video video = videoList.get(j);
                //判断小节里的章节id是否和当前章节id相等
                if(chapterVo.getId().equals(video.getChapterId())){
                    //创建封装的小节对象
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    //将其加入到对象中
                    videoVoList.add(videoVo);
                }
            }
            //将小节集合放进去
            chapterVo.setChildren(videoVoList);
        }

        return finalList;
    }

    /**
     * 删除章节信息
     * 如果章节下没有小节信息，就删除
     * 否则不删除
     * @param chapterId
     * @return
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        if(count>0){
            throw new GuliException(20001,"删除章节信息失败");
        }else{
            int index = baseMapper.deleteById(chapterId);
            return index>0;
        }
    }

    @Override
    public void deleteByCourseId(String courseId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
